using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Text.RegularExpressions;
using PluginHandler.Database;

namespace OraclePlugin.Database
{
    public class DatabaseTableColumn : IDatabaseTableColumn
    {
        private DatabaseHandler _databaseHandler;
        private IDatabaseObject _parentDatabaseObject;
        private DbType _dbType;
        private string _name;
        private string _schemaType;

        internal DatabaseTableColumn(DatabaseHandler databaseHandler, IDatabaseObject parentDatabaseObject, string name, DbType dbType, string schemaType)
        {
            _databaseHandler = databaseHandler;
            _parentDatabaseObject = parentDatabaseObject;
            _name = name;
            _dbType = dbType;
            _schemaType = schemaType;
        }

        internal static DbType ConvertStringToDbType(string schemaType)
        {
            if (Regex.IsMatch(schemaType, @"^CHAR(\(\d\))*$"))
                return DbType.AnsiStringFixedLength;

            if (Regex.IsMatch(schemaType, @"^FLOAT(\(\d\))*$"))
                return DbType.Decimal;

            if (Regex.IsMatch(schemaType, @"^INTERVAL DAY\(\d\) TO SECOND\(\d\)$"))
                return DbType.DateTimeOffset;
            
            if (Regex.IsMatch(schemaType, @"^INTERVAL YEAR\(\d\) TO MONTH"))
                return DbType.AnsiStringFixedLength;

            if (Regex.IsMatch(schemaType, @"^NUMBER(\(\d\,\d\))*$"))
                return DbType.Decimal;

            if (Regex.IsMatch(schemaType, @"^NVARCHAR2(\(\d\))*$"))
                return DbType.String;

            if (Regex.IsMatch(schemaType, @"^RAW(\(\d\))*$"))
                return DbType.Byte;

            if (Regex.IsMatch(schemaType, @"^TIMESTAMP(\(\d\))*$"))
                return DbType.DateTime;

            if (Regex.IsMatch(schemaType, @"^TIMESTAMP(\(\d\))* WITH LOCAL TIME ZONE$"))
                return DbType.DateTime;

            if (Regex.IsMatch(schemaType, @"^TIMESTAMP(\(\d\))* WITH TIME ZONE$"))
                return DbType.DateTime;

            if (Regex.IsMatch(schemaType, @"^VARCHAR2(\(\d\))*$"))
                return DbType.AnsiString;

            switch (schemaType.ToUpper())
            {
                case "BFILE":           return DbType.Byte;
                case "BINARY_DOUBLE":   return DbType.Double;
                case "BINARY_FLOAT":    return DbType.Single;
                case "BLOB":            return DbType.Byte;
                case "CLOB":            return DbType.String;
                case "DATE":            return DbType.DateTime;
                case "LONG":            return DbType.String;
                case "LONG RAW":        return DbType.Byte;
                case "NCLOB":           return DbType.String;
                case "XMLTYPE":         return DbType.String;
                case "ROWID":           return DbType.String;

                default:                return DbType.Object;   
                                                /*throw new NotImplementedException(
                                                "Datatype '" + 
                                                schemaType + 
                                                "' cannot be converted to enum!");*/ 
            }
        }

        #region IDatabaseTableColumn Member

        public string Name
        {
            get 
            { 
                return _name; 
            }
        }

        public DbType DbType
        {
            get 
            { 
                return _dbType; 
            }
        }

        public string NativeDbTypeName
        {
            get 
            {
                return _schemaType;
            }
        }

        #endregion
    }
}
